#!/bin/bash

cur_path=$0
rpc_proto_file=$1 # rpc主proto文件输入
rpc_file_simple_name="${rpc_proto_file##*/}"

if [ "$rpc_proto_file" == "" ]; then
    echo "rpc file is empty"
    echo "eg."
    echo "      ./protogen.sh  xxx.proto"
    exit 128
fi

root_path=${cur_path%/*/*}
pubpb_path="$root_path/a_config/pb/"

# # 清理并重新导入公共proto文件
# echo "------------------------------------------------------"
# echo "---delete org files"
# delete_list=`ls pub_*.proto`
# echo $delete_list
# rm $delete_list
# rm *.go
# cp $pubpb_path/*.proto .

# pubpbnames=`ls pub_*.proto`
# for pubpbname in $pubpbnames
# do
#     mv $pubpbname "$rpc_file_simple_name.$pubpbname"
# done


echo "------------------------------------------------------"

# 导出除主文件之外的所有文件
normal_protos=`ls *.proto`
for normalpt in $normal_protos
do
    # echo "$rpc_file_simple_name" "$normalpt"
    if [ "$rpc_file_simple_name" != "$normalpt" ]; then
        echo "parse pub proto file:   $normalpt"
        protoc $normalpt --go_out=../
    fi
done


# 导出rpc主文件
echo "------------------------------------------------------"
echo "parse rpc proto file:   $rpc_proto_file"
goctl rpc protoc $rpc_proto_file --go_out=../ --go-grpc_out=../ --zrpc_out=../ --style=goZero

# delete_list=`ls $rpc_proto_shortname.pub_*.proto`
# echo $delete_list
# rm $delete_list
